require('../../assets/css/index.less');
require('./info.less');
require('./iconfont/iconfont.css');
//引入axios
const request = require('../../xios/axios')

window.onload = () => {
    let img = document.querySelector('.img22')
    let uploade = document.querySelector('#uploade')
    let nicheng = document.querySelector('#nicheng')
    let sex = document.querySelector('#sex')
    // renderTabbar()
    //.获取当前用户个人信息
    const API = 'http://fitness.h5.itsource.cn:3701'
    request.get('/api/user/info').then((res => {
        let { errno, data } = res.data
        console.log(res.data);
        if (errno === 0) {
            img.src = API + data.imgUrl
            nicheng.value = data.nickName
            sex.textContent = data.gender ?? '请选择性别'
            shegnri.textContent = data.birthday ?? '请选择生日'
            text.value = data.sign
        }
    }))

    //获取用户信息
    function fa() {
        return request('/api/user/info').then((err => {
            let { errno, data } = err.data
            // console.log(err);
        }))

    }
    fa()
    // 监听
    uploade.addEventListener('change', e => {
        // console.log(e.target.files[0]);
        //创建Fromdata对象 添加文件
        const formData = new FormData()
        formData.append('file', e.target.files[0])
        // 发送请求  
        request.post('/api/circles/uploadImg',formData).then((err => {
            let { errno, data } = err.data
            if (errno === 0) {
                img.src = API + data.url
            }
        }))
    })
    // 上传图片
    let upt = document.querySelector('#upt')
    console.log(upt);
    upt.addEventListener('click', function () {
        console.log(1);
        uploade.click()
    })
    // 生日
    let showDatePicker = document.querySelector('#showDatePicker')
    let shegnri = document.querySelector('#shegnri')
    showDatePicker.addEventListener('click', function () {
        weui.datePicker({
            start: 1990,
            end: new Date().getFullYear(),
            onConfirm: function (result) {
                console.log(result);
                shegnri.textContent = result.map(v => String(v.value).padStart(2,'0')).join('-')
            },
            title: '时间'
        });
    })

    let showPicker = document.querySelector('#showPicker')

    showPicker.addEventListener('click', () => {
        weui.picker([{
            label: '男',
            value: 0
        }, {
            label: '女',
            value: 1
        }
        ], {

            onConfirm: function (result) {
                console.log(result);
                sex.textContent = result[0].label
                
            },
            title: '性别选项'
        })
            ;
    })
    // 获取省份
    let showPicker2 = document.querySelector('#showPicker2')
    let porvse = []
    console.log(porvse);
    // 获取省份
    function shen() {
        request.get('api/shared/province').then((err => {
            let { errno, data } = err.data
            console.log(err.data);

            if (errno === 0) {
                porvse = data.map(v => {
                    return {
                        label: v.name,
                        value: v.id
                    }
                })
                // console.log(porvse);
            }
        }))
    }
    shen()
    let a = []
    let provinceId='';
    let shenfen = document.querySelector('#shenfen')
    showPicker2.addEventListener('click', () => {
        weui.picker(porvse, {
            // onChange: function (result) {
            //     console.log(result);
            // },
            onConfirm: function (result) {
                console.log(result);
                shenfen.textContent = result.map(v => v.label)
                a = result[0].value
                fcc(a)
                // provinceId=result[0].value
            },
            title: '省份'

        }

        )


    })
    let tokenID=null //用于用户存储的上一级
    // 获取市级
    let acc = []
    let cityId='';
    function fcc(provinceId) {
        request.get('/api/shared/city', {
            params: {
                provinceId,
            },
        }).then((err => {
            console.log(provinceId);
            let { errno, data } = err.data
            console.log(err.data);
            if (errno === 0) {
                acc = data.map(v => {
                    return {
                        label: v.name,
                        value: v.id
                    }
                })
                console.log(acc);
            }
        }))
    }
    fcc()
    let chegnhsi = document.querySelector('#chegnhsi')
    let showPicker3 = document.querySelector('#showPicker3')
    function ooo(){
        weui.picker(acc, {

            // onChange: function (result) {
            //     console.log(result);
            // },
            onConfirm: function (result) {
                console.log(result);
                chegnhsi.textContent = result[0].label
                 cityId=result[0].value
            },
            title: '城市'
        })
    }
    // 赋值给tokenID把以前数据保存起来
   
    // 清除事件，清除上一次的点击事件数据
    showPicker3.removeEventListener('click',tokenID)
    tokenID=ooo
    // 获取数据后在绑定选择器渲染数据
    showPicker3.addEventListener('click', ooo)
    //文本
    let text = document.querySelector('#text');
    let san = document.querySelector('#san');
    text.addEventListener('input', function () {
        // console.log(text.value.length);
        san.textContent = text.value.length
    })
    // 监听提交按钮
    let submit = document.querySelector('#submit')
    submit.addEventListener('click', function () {
        request.post('/api/user/changeInfo', {
            nickName: nicheng.value,
            gender: sex.textContent,
            provinceId:a,
            cityId:cityId,
            birthday:shegnri.textContent,
            imgUrl:img.src.replace(API,''),
            sign:text.value
        })
        .then((res => {
            let { errno, data } = res.data
            console.log(data);
            if(errno===0){
                // 更新token
                localStorage.getItem('token','Bearer '+data.token)
            }
        }))
    })
    request.get('/api/user/info').then(trr=>{
        let {errno,data}=trr.data
        console.log(trr.data);
        if(errno===0){
            img.src = API + data.imgUrl
            nicheng.value = data.nickName
            sex.textContent = data.gender ?? '请选择性别'
            shegnri.textContent = data.birthday ?? '请选择生日'
            text.value = data.sign
            request.get('/api/shared/city',{
                params: {
                    provinceId:data.provinceId
                } 
            }
           ).then(res=>{
            //  res.data.data.map(v=>{
            //      if(v.id===)
            //  })
             console.log(res.data.data);
           })
        }
    })
}  


